package com.sychip.fhc.app.pref

import com.fhc.usb.UsbDeviceDto
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import com.sychip.fhc.AppType
import com.sychip.fhc.app.wsn.screen.FilterDto
import kotlinx.coroutines.flow.Flow


val moshi: Moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
val acJsonAdapter: JsonAdapter<ACStatus> = moshi.adapter(ACStatus::class.java).lenient()


const val RssiFilterKey = "RssiFilterKey"
const val MapSizeChangeReminderMe = "MapSizeChangeReminderMe"
const val ForTabletReminderMe = "ForTabletReminderMe"
const val AppSettingKey = "AppSettingKey"
val appSettingJsonAdapter: JsonAdapter<AppSetting> = moshi.adapter(AppSetting::class.java).lenient()

const val UsbDeviceKey = "UsbDeviceKey"
val usbDeviceJsonAdapter: JsonAdapter<UsbDeviceDto> = moshi.adapter(UsbDeviceDto::class.java).lenient()

const val FilterKey = "FilterKey"
val filterJsonAdapter: JsonAdapter<FilterDto> = moshi.adapter(FilterDto::class.java).lenient()
enum class ACMode {
    HEAT, COOL, FAN, DRY, AUTO
}
enum class FanSpeed {
    LOW, MED, HIGH, AUTO
}
data class ACStatus(var powerOn: Boolean = false, var temp: Int = 18, var mode: ACMode = ACMode.AUTO, var fanSpeed: FanSpeed = FanSpeed.AUTO){

    fun isMode(mode:ACMode): Boolean{
        return this.powerOn && this.mode == mode
    }
}

data class AppSetting(var appType: AppType = AppType.WSN, var avgNums:Int = 4, var rssiMins:Int = 5, var logEnable: Boolean = false, var mockMode: Boolean = false){

}


interface PrefDataStoreIf {

    fun getFirstLaunch(): Flow<Boolean>

    suspend fun saveFirstLaunch(isFirstLaunch: Boolean)


    suspend fun getAppType(): AppType

    suspend fun saveAppType(appType:AppType)


    suspend fun getCurrentUsbId(): String

    suspend fun setCurrentUsbId(usbId:String)

    fun getACStatus(devId:String): Flow<ACStatus?>

    suspend fun saveACStatus(devId:String, json: ACStatus)

    fun getAppSettings(): Flow<AppSetting>

    suspend fun saveAppSettings(json: AppSetting)

     fun getRssiFilter(): Flow<String>
    suspend fun saveRssiFilter(filter:String)

    fun getFilterDto(): Flow<FilterDto?>
    suspend fun saveFilterDto(json: FilterDto)

    fun getMapSizeChangeReminderMe(): Flow<Boolean>
    suspend fun saveMapSizeChangeReminderMe(remind:Boolean)
    suspend fun getRemindNotTablet(): Boolean
     suspend fun setRemindNotTablet(remind: Boolean)
    fun getUsbDevice(): Flow<UsbDeviceDto?>
    suspend fun saveUsbDevice(json: UsbDeviceDto)

    fun compareUsbDevice(usb1:UsbDeviceDto, usb2:UsbDeviceDto):Boolean

}